package tool;

import org.apache.commons.lang3.StringUtils;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.regex.Pattern;

/**
 * @Description:
 * @Author: yjj
 * @Date: 2025/8/18 14:03
 */
public class Main4 {
    public static void main(String[] args) throws URISyntaxException {
        String url = "https://10.68.33.250:31090/resource/v1/servers/shut-down/4760566730416939139";
        String pattern = "^/resource/v1/servers/shut-down/[0-9]+$";
        System.out.println(checkUrlByPattern(url, pattern));
    }

    /**
     * 校验url是否符合pattern规范
     *
     * @param url
     * @param pattern
     * @return
     */
    public static boolean checkUrlByPattern(String url, String pattern) {
        if (StringUtils.isEmpty(url) || StringUtils.isEmpty(pattern)) {
            return false;
        }
        try {
            String path = new URI(url).getPath(); // 推荐方式获取路径
            Pattern pat = Pattern.compile(pattern);
            return pat.matcher(path).matches();
        } catch (URISyntaxException e) {
            return false;
        }
    }
}

